import { _decorator, Button, Component, EditBox, instantiate, Node, Prefab, resources } from 'cc';
import BaseView from '../../../platform/ui/BaseView';
import { MenuBtn } from './components/MenuBtn';
import { Menu_Btns, ViewLayerDefine } from '../../common/enums';
import { HomeViewModel } from './HomeViewModel';
import { ResManager } from '../../../platform/resources/ResMgr';
import { bind, uiview } from '../../../platform/ui/UIDecorator';
import { Menu_Btn_Flex } from './Menu_Btn_Flex';
import { LogMgr } from '../../../platform/log/LogMgr';
import { TbRoot } from '../../../platform/excel_table/TbRoot';
import { Tb_GameListContainer } from '../../tb/Tb_GameList';
import { GameItem } from './components/GameItem';
const { ccclass, property } = _decorator;





@uiview({
    prefabPath:"prefab/ui/home/HomeView",
    layer:ViewLayerDefine.Main,
    dataModel:HomeViewModel,
    single:true
})
@ccclass('HomeView')
export class HomeView extends BaseView<HomeViewModel,null> {

    //=================内容标签=====================
    @bind(Node)
    game_all:Node = null
    @bind(Node)
    game_all_list:Node = null
    @bind(Node)
    game_played:Node = null
    @bind(Node)
    mall:Node = null
    @bind(Node)
    achivement:Node = null
    @bind(Node)
    my:Node = null

    //=================菜单按钮=====================
    @bind(Node)
    menu_bar:Node = null
    @bind(Menu_Btn_Flex, "&menu_bar")
    menu_bar_flex:Menu_Btn_Flex = null


    menuNodesMap:Map<number,MenuBtn> = new Map()
    contentTabNodes:Map<number,Node> = new Map()
    protected init(): void {
        this.initContents()//内容标签
        this.initMenuBtns()//底部菜单按钮
        this.addDataListener()

    }

    async initContents() {
        this.contentTabNodes.set(Menu_Btns.All,this.game_all)
        this.contentTabNodes.set(Menu_Btns.Played,this.game_played)
        this.contentTabNodes.set(Menu_Btns.Mall,this.mall)
        this.contentTabNodes.set(Menu_Btns.Achivement,this.achivement)
        this.contentTabNodes.set(Menu_Btns.My,this.my)

        //游戏列表
        this.game_all_list.removeAllChildren()
        let container = TbRoot.ins.getTbContainer(Tb_GameListContainer)
        for (let index = 0; index < container.cfgs.length; index++) {
            const cfg = container.cfgs[index];
            let prefab = await ResManager.inst.loadBundleRes("resources/prefab/ui/home/game_item",Prefab)
            let item = instantiate(prefab) as Node
            let cmp = item.getComponent(GameItem)
            if(!cmp){
                cmp = item.addComponent(GameItem)
            }
            this.registerComponent(cmp,this)
            this.game_all_list.addChild(item)
            cmp.init(cfg)
        }

    }
    async initMenuBtns() {
        this.menu_bar.removeAllChildren()
        for (let i = 0; i < 5; i++) {
            let btnPrefab = await ResManager.inst.loadBundleRes("resources/prefab/ui/home/menu_btn", Prefab)
            let btn = instantiate(btnPrefab) as Node
            let cmp = btn.getComponent(MenuBtn)
            if(!cmp){
                cmp = btn.addComponent(MenuBtn)
            }
            this.registerComponent(cmp,this)
            this.menu_bar.addChild(btn)
            cmp.init(i)
            this.menuNodesMap.set(i,cmp)
        }
        this.menu_bar_flex.resetNodes()
    }
    addDataListener() {
        this.watchData(()=>this.dataModel.selectMenu.value,(value: any, oldValue: any)=>{
            LogMgr.info("选中按钮变化："+value)
            this.contentTabNodes.forEach((node,id)=>{
                if(id === value){
                    node.active = true
                }else{
                    node.active = false
                }
            })
            this.menuNodesMap.forEach((cmp,id)=>{
                cmp.updateStatus()
            })
        })
    } 
    protected show(): void {

        this.menu_bar_flex.resetNodes()
        //根据最新数据刷新界面内容
        this.refreshContent()
    }
    refreshContent() {
        
    }

}



